import { Random } from "./RandomNormal";

export class Multinomial {
    private readonly _random: Random;

    constructor(seed: number) {
        this._random = new Random(seed);
    }
    public sample(cmf: number[]): number;
    public sample(cmf: number[], branchSize?: number): number;
    public sample(cmf: number[], branchSize?: number): number {

        if (!branchSize) {
            branchSize = cmf.length;
        }

        const p = this._random.nextFloat() * cmf[branchSize - 1];
        let cls = 0;
        while (cmf[cls] < p) {
            ++cls;
        }

        return cls;
    }

}